% this code is for Jumps and Post-FOMC Announcement Returns in Currency Markets
% this helps us make table 4 (time series sorting) and table 8

clear
clc
load(using data including 12hr measures)

%% 1. time series sorting (table 4)

exporttimesort=nan(nf,8);
addexporttimesort=nan(nf,3);
tp=0;
tprxe=hwrxe;
tprxe(:,19)=[];
tprxe(:,17)=[];
tprxe(:,12)=[];
tprxe(:,6)=[];
tprxe(:,2)=[];

for j=1:nhlf
    if hwindicator(j,9)>0   % indicator for effective 12 hr window obs.
        tp=tp+1;
        exporttimesort(tp,1)=nanmean(hwrxe(j+1,1:nc),2);
        exporttimesort(tp,2)=nanmean(tprxe(j+1,:),2);
        exporttimesort(tp,3)=hwrxe(j+1,nc+1);
        exporttimesort(tp,4)=nanmean(hwresirxe(j+1,1:nc),2);
        exporttimesort(tp,5)=hwsurp(j,1);   % monetary policy surprise
        exporttimesort(tp,6)=hwgvol(j,1);   % currency market volatility
        exporttimesort(tp,7)=hwfguiance(j,1); % forward guidance
        exporttimesort(tp,9)=hwswap(j,1);   % 10yr swap rate
        addexporttimesort(tp,1)=nanmean(sum(hwrxe(j-3:j-1,1:nc),1),2);
        addexporttimesort(tp,2)=nanmean(hwrxe(j-1,1:nc),2);
        addexporttimesort(tp,3)=nanmean(hwrxe(j,1:nc),2);  
    end    
end
clear j

% export (add)exportimesort to MS Excel with window numbering, then using
% Excel's Data/Filter, we sort 12 hr windows on each measure
% to get mean returns, we use AVERAGE function, and to compute standard
% deviation for standard errors, we use STDEV functions

%% 2. cross sectional sorting (table 8)

% 1.1) making 12 hour bas measure

hwbas=nan(nhlf,nc+1);
temp=nanmean(bas,2);
tpbas=[bas temp].*lreie;

for hw=1:nhlf
    tempie=lreie(winstart(hw,1):winstart(hw,1)+npday/2-1,:);
    tempbas=tpbas(winstart(hw,1):winstart(hw,1)+npday/2-1,:);
    tpsumie=nansum(tempie,1);
    tpmeanbas=nanmean(tempbas,1);
    
    for i=1:nc+1
        if tpsumie(1,i)>npday/4-1
            hwbas(hw,i)=tpmeanbas(1,i);
        end
    end
    clear i tempie tempbas tpsumie tpmeanbas
end
clear hw temp tpbas

hwbasregular=hwbas;
for i=1:nc+1
    for j=1:nhlf
        if hwbasregular(j,i)>1
            hwbasregular(j,i)=nan;
        end
    end
    clear j
end
clear i

% 1.2) sizing
nf=0.5*nansum(usfomc(:,1),1);   % # of total fomc announcements

% 0.3) selection matrix (fomc+0:+12 / fomc+12:+24)
selectpreone=nan(nhlf,nc);
selectpostone=nan(nhlf,nc);
selectposttwo=nan(nhlf,nc);

for j=1:nhlf
    if hwindicator(j,8)>0
        selectpreone(j,:)=1;
    end
    if hwindicator(j,9)>0
        selectpostone(j,:)=1;
    end
    if hwindicator(j,10)>0
        selectposttwo(j,:)=1;
    end    
end
clear j

% 1.3) bas sorting

basbig=nan(nf,nc);
basmid=nan(nf,nc);
baslow=nan(nf,nc);

tempone=nan(nf,nc);
tempsrxe=nan(nf,nc);
tempsresi=nan(nf,nc);
templaglong=nan(nf,nc);
templagshort=nan(nf,nc);
temprxe=nan(nf,nc);

tp=0;

for j=1:nhlf
    if selectpostone(j,1)>0
        tp=tp+1;
        tempone(tp,:)=hwbas(j,1:nc);
        tempsrxe(tp,:)=hwrxe(j+1,1:nc);
        tempsresi(tp,:)=hwresirxe(j+1,1:nc);
        templaglong(tp,:)=sum(hwrxe(j-3:j-1,1:nc),1);
        templagshort(tp,:)=hwrxe(j-1,1:nc);
        temprxe(tp,:)=hwrxe(j,1:nc);
    end
end
clear j tp

tpcountone=ones(size(tempone,1),size(tempone,2))-isnan(tempone);
tpcountsrxe=ones(size(tempsrxe,1),size(tempsrxe,2))-isnan(tempsrxe);
tpcountsresi=ones(size(tempsresi,1),size(tempsresi,2))-isnan(tempsresi);

for j=1:nf
        if sum(tpcountone(j,:),2)>10 && sum(tpcountsrxe(j,:),2)>10
            tprank=tiedrank(tempone(j,:));
            for i=1:nc
                if tprank(1,i)>max(tprank(1,:))-3.3
                    basbig(j,i)=1;
                end
                if tprank(1,i)<4.3
                    baslow(j,i)=1;
                end
                if tprank(1,i)>=0
                    basmid(j,i)=1;
                    if basbig(j,i)==1 || baslow(j,i)==1
                        basmid(j,i)=nan;
                    end
                end
            end
            clear i
        end
        if sum(tpcountone(j,:),2)>14 && sum(tpcountsrxe(j,:),2)>14
            tprank=tiedrank(tempone(j,:));
            for i=1:nc
                if tprank(1,i)>max(tprank(1,:))-4.3
                    basbig(j,i)=1;
                end
                if tprank(1,i)<5.3
                    baslow(j,i)=1;
                end
                if tprank(1,i)>=0
                    basmid(j,i)=1;
                    if basbig(j,i)==1 || baslow(j,i)==1
                        basmid(j,i)=nan;
                    end
                end
            end
            clear i
        end
        clear tprank
end
clear j tpcountone tpcounttwo tpcountsrxe tpcountsresi

basbigbas=basbig.*tempone;
basmidbas=basmid.*tempone;
baslowbas=baslow.*tempone;

basbigsrxe=basbig.*tempsrxe;
basmidsrxe=basmid.*tempsrxe;
baslowsrxe=baslow.*tempsrxe;

basbigsresi=basbig.*tempsresi;
basmidsresi=basmid.*tempsresi;
baslowsresi=baslow.*tempsresi;

basbiglaglong=basbig.*templaglong;
basmidlaglong=basmid.*templaglong;
baslowlaglong=baslow.*templaglong;

basbiglagshort=basbig.*templagshort;
basmidlagshort=basmid.*templagshort;
baslowlagshort=baslow.*templagshort;

basbigrxe=basbig.*temprxe;
basmidrxe=basmid.*temprxe;
baslowrxe=baslow.*temprxe;

basbigbasseries=nanmean(basbigbas,2);
basmidbasseries=nanmean(basmidbas,2);
baslowbasseries=nanmean(baslowbas,2);
bashmlbasseries=basbigbasseries-baslowbasseries;

basbigsrxeseries=nanmean(basbigsrxe,2);
basmidsrxeseries=nanmean(basmidsrxe,2);
baslowsrxeseries=nanmean(baslowsrxe,2);
bashmlsrxeseries=basbigsrxeseries-baslowsrxeseries;

basbigsresiseries=nanmean(basbigsresi,2);
basmidsresiseries=nanmean(basmidsresi,2);
baslowsresiseries=nanmean(baslowsresi,2);
bashmlsresiseries=basbigsresiseries-baslowsresiseries;

basbiglaglongseries=nanmean(basbiglaglong,2);
basmidlaglongseries=nanmean(basmidlaglong,2);
baslowlaglongseries=nanmean(baslowlaglong,2);
bashmllaglongseries=basbiglaglongseries-baslowlaglongseries;

basbiglagshortseries=nanmean(basbiglagshort,2);
basmidlagshortseries=nanmean(basmidlagshort,2);
baslowlagshortseries=nanmean(baslowlagshort,2);
bashmllagshortseries=basbiglagshortseries-baslowlagshortseries;

basbigrxeseries=nanmean(basbigrxe,2);
basmidrxeseries=nanmean(basmidrxe,2);
baslowrxeseries=nanmean(baslowrxe,2);
bashmlrxeseries=basbigrxeseries-baslowrxeseries;

repsortbas=nan(7,5);    % panel a
temp=[baslowbasseries basmidbasseries basbigbasseries bashmlbasseries];
repsortbas(1,1:4)=nanmean(temp,1);
repsortbas(1,4)=repsortbas(1,3)-repsortbas(1,1);
repsortbas(1,5)=sqrt(nf)*repsortbas(1,4)/nanstd(temp(:,4),1);
clear temp

temp=[baslowsrxeseries basmidsrxeseries basbigsrxeseries bashmlsrxeseries];
repsortbas(2,1:4)=nanmean(temp,1);
repsortbas(2,4)=repsortbas(2,3)-repsortbas(2,1);
repsortbas(2,5)=sqrt(nf)*repsortbas(2,4)/nanstd(temp(:,4),1);
clear temp

temp=[baslowsresiseries basmidsresiseries basbigsresiseries bashmlsresiseries];
repsortbas(3,1:4)=nanmean(temp,1);
repsortbas(3,4)=repsortbas(3,3)-repsortbas(3,1);
repsortbas(3,5)=sqrt(nf)*repsortbas(3,4)/nanstd(temp(:,4),1);
clear temp

temp=[baslowlaglongseries basmidlaglongseries basbiglaglongseries bashmllaglongseries];
repsortbas(5,1:4)=nanmean(temp,1);
repsortbas(5,4)=repsortbas(5,3)-repsortbas(5,1);
repsortbas(5,5)=sqrt(nf)*repsortbas(5,4)/nanstd(temp(:,4),1);
clear temp

temp=[baslowlagshortseries basmidlagshortseries basbiglagshortseries bashmllagshortseries];
repsortbas(6,1:4)=nanmean(temp,1);
repsortbas(6,4)=repsortbas(6,3)-repsortbas(6,1);
repsortbas(6,5)=sqrt(nf)*repsortbas(6,4)/nanstd(temp(:,4),1);
clear temp

temp=[baslowrxeseries basmidrxeseries basbigrxeseries bashmlrxeseries];
repsortbas(7,1:4)=nanmean(temp,1);
repsortbas(7,4)=repsortbas(7,3)-repsortbas(7,1);
repsortbas(7,5)=sqrt(nf)*repsortbas(7,4)/nanstd(temp(:,4),1);
clear temp

clear tempone

% 2) jump volatility sorting

sorthwrxejupv=nan(nhlf,nc);
sorthwresirxejupv=nan(nhlf,nc);
sorthwrxejdnv=nan(nhlf,nc);
sorthwresirxejdnv=nan(nhlf,nc);
sorthwrxenojp=nan(nhlf,nc);
sorthwresirxenojp=nan(nhlf,nc);
sortjupv=nan(nhlf,nc);
sortjdnv=nan(nhlf,nc);

for j=1:nhlf
    if hwindicator(j,9)>0
        for i=1:nc
            if hwjupv(j,i)>0
                sorthwrxejupv(j,i)=hwrxe(j+1,i);
                sorthwresirxejupv(j,i)=hwresirxe(j+1,i);
                sortjupv(j,i)=hwjupv(j,i);
            end
            if hwjdnv(j,i)>0 && hwresirxe(j+1,i)<0.70
                sorthwrxejdnv(j,i)=hwrxe(j+1,i);
                sorthwresirxejdnv(j,i)=hwresirxe(j+1,i);
                sortjdnv(j,i)=hwjdnv(j,i);
            end
            if hwjupv(j,i)==0 && hwjdnv(j,i)==0
                sorthwrxenojp(j,i)=hwrxe(j+1,i);
                sorthwresirxenojp(j,i)=hwresirxe(j+1,i);
            end
        end
        clear i
    end
end
clear j

repsortjvol=nan(3,5);

tprxejupv=nanmean(sorthwrxejupv,2);
tpresirxejupv=nanmean(sorthwresirxejupv,2);
tprxejdnv=nanmean(sorthwrxejdnv,2);
tpresirxejdnv=nanmean(sorthwresirxejdnv,2);
tprxenojp=nanmean(sorthwrxenojp,2);
tpresirxenojp=nanmean(sorthwresirxenojp,2);
tprxemx=[tprxenojp tprxejupv tprxejdnv];
tprxediff=tprxejdnv-tprxenojp;
tprxediffstd=nanstd(tprxediff,1);
tpresirxemx=[tpresirxenojp tpresirxejupv tpresirxejdnv];
tpresidiff=tpresirxejdnv-tpresirxenojp;
tpresidiffstd=nanstd(tpresidiff,1);

repsortjvol(2,1:3)=nanmean(tprxemx,1);
repsortjvol(2,4)=repsortjvol(2,3)-repsortjvol(2,1);
repsortjvol(2,5)=sqrt(nf)*repsortjvol(2,4)/tprxediffstd;

repsortjvol(3,1:3)=nanmean(tpresirxemx,1);
repsortjvol(3,4)=repsortjvol(3,3)-repsortjvol(3,1);
repsortjvol(3,5)=sqrt(nf)*repsortjvol(3,4)/tpresidiffstd;

repsortjvol(1,1)=0;
repsortjvol(1,2)=nanmean(nanmean(sortjupv,2),1);
repsortjvol(1,3)=nanmean(nanmean(sortjdnv,2),1);
repsortjvol(1,4)=repsortjvol(1,3);






